package com.sdy.kzyy.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.kzyy.biz.dto.UserDTO;
import com.sdy.kzyy.biz.model.PersonalInformation;
import com.sdy.kzyy.biz.mapper.PersonalInformationMapper;
import com.sdy.kzyy.biz.service.PersonalInformationService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-03-05
 */
@Slf4j
@Service
public class PersonalInformationServiceImpl extends BaseServiceImpl<PersonalInformation> implements PersonalInformationService {
    @Autowired
    private PersonalInformationMapper personalInformationMapper;

    @Override
    public void saveOrUpdateInformation(UserDTO userDTO) {
        QueryWrapper<PersonalInformation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userDTO.getUserId());
        wrapper.eq("card", userDTO.getCardid());
        wrapper.eq("name", userDTO.getName());
        List<PersonalInformation> personalInformationList = list(wrapper);
        if(personalInformationList.size() == 0){
            PersonalInformation personalInformation = new PersonalInformation();
            personalInformation.setCard(userDTO.getCardid());
            personalInformation.setUserId(userDTO.getUserId());
            personalInformation.setName(userDTO.getName());
            personalInformation.setPhone(userDTO.getPhone());
            save(personalInformation);
        }else {
            PersonalInformation personalInformation = new PersonalInformation();
            personalInformation.setPhone(userDTO.getPhone());
            update(personalInformation, new UpdateWrapper<PersonalInformation>().eq("user_id", userDTO.getUserId())
                    .eq("card", userDTO.getCardid()));
        }
    }

}
